package com.yuexun.dhdoctor.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by chenss on 2018/4/11 0011.
 */

public class DateUtil {
    /**
     * 得到本周周一
     *
     * @return yyyy-MM-dd
     */
    public static String getMondayOfThisWeek(Calendar c) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(c.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        calendar.add(Calendar.DATE, -day_of_week + 1);
        return formatter.format(calendar.getTime());
    }

    /**
     * 得到本周周一
     *
     * @return MM.dd
     */
    public static String getMondayOfThisWeek_Title(Calendar c) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(c.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("MM.dd", Locale.getDefault());
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        calendar.add(Calendar.DATE, -day_of_week + 1);
        return formatter.format(calendar.getTime());
    }

    public static Calendar getMondayOfThisWeek_Calendar(Calendar c) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(c.getTime());
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        calendar.add(Calendar.DATE, -day_of_week + 1);
        return calendar;
    }

    /**
     * 得到本周周日
     *
     * @return yyyy-MM-dd
     */
    public static String getSundayOfThisWeek(Calendar c) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(c.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        calendar.add(Calendar.DATE, -day_of_week + 7);
        return formatter.format(calendar.getTime());
    }

    /**
     * 得到本周周日
     *
     * @return MM.dd
     */
    public static String getSundayOfThisWeek_Title(Calendar c) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(c.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("MM.dd", Locale.getDefault());
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        calendar.add(Calendar.DATE, -day_of_week + 7);
        return formatter.format(calendar.getTime());
    }

    /**
     * 得到本周某天
     *
     * @return MM.dd
     */
    public static String getDayOfThisWeek(Calendar c, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(c.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        calendar.add(Calendar.DATE, -day_of_week + day);
        return formatter.format(calendar.getTime());
    }


    public static int compare_date(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }


    public static Date getFirstDayOfWeek(Date nowDate) {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowDate);
        int week = calendar.get(Calendar.DAY_OF_WEEK);

        Date firstDay;
        if (week == Calendar.SUNDAY) {
            calendar.add(Calendar.DATE, -6);
            firstDay = calendar.getTime();
        } else {
            calendar.add(Calendar.DATE, -(week - 2));
            firstDay = calendar.getTime();
        }

        return firstDay;
    }

    public static Date getLastDayOfWeek(Date nowDate) {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowDate);
        int week = calendar.get(Calendar.DAY_OF_WEEK);

        Date lastDay;
        if (week == Calendar.SUNDAY) {
            lastDay = nowDate;
        } else {
            calendar.add(Calendar.DATE, (7 - week + 1));
            lastDay = calendar.getTime();
        }

        return lastDay;
    }

    /**
     * @param nowDate
     * @return 格式(yyyy年MM月第N周)
     */
    public static String getWeekOfMonth(Date nowDate) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(android.text.format.DateFormat.format("yyyy年MM月", nowDate));

        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowDate);
        switch (calendar.get(Calendar.WEEK_OF_MONTH)) {
            case 1:
                stringBuffer.append("第一周");
                break;
            case 2:
                stringBuffer.append("第二周");
                break;
            case 3:
                stringBuffer.append("第三周");
                break;
            case 4:
                stringBuffer.append("第四周");
                break;
            case 5:
                stringBuffer.append("第五周");
                break;
            case 6:
                stringBuffer.append("第六周");
                break;
        }

        return stringBuffer.toString();
    }

    /**
     * 得到几天前的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    /**
     * 得到几天后的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }
}
